#include "graphicsView.h"
#include <QApplication>

GraphicsView::GraphicsView(QGraphicsView *parent) :
    QGraphicsView(parent)
{
    resize(850,850);
    setFixedSize(850,850);


    scene = new QGraphicsScene();

    setScene(scene);
    //setRenderHint(QPainter::Antialiasing);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scene->setSceneRect(-425,-425,850,850);


    timer = new QTimer();
    timer->start(10);

    for(int i = 0; i < 85; ++i){
        for(int j = 0; j < 121; ++++j){
            Circle* circle = new Circle();
            circle->setObjectName("circle");
            scene->addItem(circle);
            circle->setPos(10*i - 425 + 5, 7*j - 425 + 3);
            connect(timer, &QTimer::timeout, circle, &Circle::slotTimeout);
        }
    }
    for(int i = 0; i < 86; ++i){
        for(int j = 1; j < 122; ++++j){
            Circle* circle = new Circle();
            circle->setObjectName("circle");
            scene->addItem(circle);
            circle->setPos(10*i - 425, 7*j - 425 + 3);
            connect(timer, &QTimer::timeout, circle, &Circle::slotTimeout);
        }
    }
    for(int i = 0; i < 3; ++i)
        newTriangle();
}

GraphicsView::~GraphicsView()
{
    delete scene;
    delete timer;
}

void GraphicsView::newTriangle()
{
    Triangle* triangle = new Triangle;
    triangle->setObjectName("triangle");
    scene->addItem(triangle);
    triangle->setPos(qrand() % 850 - 425, qrand() % 850 - 425);
    connect(timer, &QTimer::timeout, triangle, &Triangle::slotGameTimer);
    connect(triangle, &Triangle::signalCheckItem, this, &newTriangle);
    connect(triangle, &Triangle::remove, this, &removeTriangle);
    scene->removeItem(text);
    text = scene->addText(QString::number(++count));
}

void GraphicsView::removeTriangle()
{
    Triangle* triangle = qobject_cast<Triangle*>(QObject::sender());
    scene->removeItem(triangle);
    delete triangle;
    scene->removeItem(text);
    text = scene->addText(QString::number(--count));
}
